package org.oschina.api.discuss.order.service;

import java.util.List;
import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.discuss.order.dao.DiscussOrderDAO;
import org.oschina.api.discuss.order.model.DiscussOrder;
import org.oschina.api.discuss.order.vo.DiscussOrderVo;
import org.oschina.bean.Result;
import org.oschina.security.ISecurity;
import org.oschina.security.annotation.Require;
import org.oschina.api.index.service.IndexService;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DiscussOrderService extends BaseService {

	@Autowired
	private DiscussOrderDAO discussOrderDAO;
	@Autowired
	private IndexService indexService;
	@Autowired
	private ISecurity security;
	
	/**
	 * 返回
	 * @return
	 */
	public List<DiscussOrderVo> findVo()
	{
		List<DiscussOrderVo> result=_findVo();
		if(!security.require(new String[]{Module.MODULE_16}, getToken(), false))
		{
			for(DiscussOrderVo discussOrder:result)
			{
				discussOrder.setId(null);
				discussOrder.setIndex(null);
				discussOrder.setCreate_time(null);
			}
		}
		return result;
	}
	
	/**
	 * 创建
	 * @param discussOrder
	 * @return
	 */
	@Require(authorization=Module.MODULE_16)
	public Result create(DiscussOrder discussOrder)
	{
		return ResultUtil.getSuccess(_create(discussOrder));
	}
	
	/**
	 * 更新
	 * @param discussOrder
	 * @return
	 */
	@Require(authorization=Module.MODULE_16)
	public Result update(DiscussOrder discussOrder)
	{
		if(_update(discussOrder)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		return ResultUtil.getSuccess(discussOrder);
	}
	
	/**
	 * 设为默认
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_16)
	public Result setDefault(int id)
	{
		DiscussOrderVo discussOrderVo=findVoById(id);
		if(discussOrderVo==null || discussOrderVo.get_default())
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		clearDefault();
		_setDefault(id);
		return ResultUtil.getSuccess(Message.UPDATE_SUCCESS);
	}
	
	/**
	 * 返回默认
	 * @return
	 */
	public DiscussOrderVo getDefault()
	{
		return discussOrderDAO.getDefault();
	}
	
	/**
	 * 取消所有默认
	 * @return
	 */
	private int clearDefault()
	{
		return discussOrderDAO.clearDefault();
	}
	
	/**
	 * 设置默认
	 * @param id
	 * @return
	 */
	private int _setDefault(int id)
	{
		return discussOrderDAO.setDefault(id);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public DiscussOrderVo findVoById(int id)
	{
		return discussOrderDAO.findVoById(id);
	}
	
	/**
	 * 更新
	 * @param discussOrder
	 * @return
	 */
	private int _update(DiscussOrder discussOrder)
	{
		return discussOrderDAO.update(discussOrder);
	}
	
	/**
	 * 创建
	 * @param discussOrder
	 * @return
	 */
	private DiscussOrder _create(DiscussOrder discussOrder)
	{
		discussOrder.setId(getIndex());
		discussOrder.set_default(false);
		discussOrder.setCreate_time(DateUtil.now());
		return discussOrderDAO.create(discussOrder);
	}

	/**
	 * 返回
	 * @return
	 */
	private List<DiscussOrderVo> _findVo()
	{
		return discussOrderDAO.findVo();
	}
	
	private int getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_DISCUSS_ORDER);
	}
}